<template>
  <template v-for="item in props.menuData">
    <!-- 无子菜单的  -->
    <el-menu-item
      v-if="!item.children || item.children?.length == 0"
      :index="`${props.index}-${item.meta.id}`"
      :key="`${props.index}-${item.meta.id}`"
      @click="handleClick(item)"
    >
      <el-icon size="20">
        <component :is="item.meta.icon"></component>
      </el-icon>
      <span>{{ item.meta.name }}</span>
    </el-menu-item>
    <!-- 有子菜单 -->
    <el-sub-menu
      v-else
      :index="`${props.index}-${item.meta.id}`"
      :key="`${props.index}-${item.meta.id}`"
    >
      <template #title>
        <el-icon size="20">
          <component :is="item.meta.icon"></component>
        </el-icon>
        <span>{{ item.meta.name }}</span>
      </template>
      <TreeMenu
        :index="`${props.index}-${item.meta.id}`"
        :menuData="item.children"
      ></TreeMenu>
    </el-sub-menu>
  </template>
</template>

<script setup>
import { useRouter } from 'vue-router';
import { useStore } from 'vuex';
// 接收的数据
const props = defineProps(['menuData', 'index']);
// console.log(props);

const router = useRouter();
const store = useStore();
// 定义点击跳转事件
const handleClick = (item) => {
  //   路由跳转
  router.push(item.meta.path);
  // 调用vuex的方法
  store.commit('addMenu', item.meta);
};
</script>

<style></style>
